import 'package:flutter/material.dart';

import 'root.router.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final pages = DggRoutes().getAllPages();
    return MaterialApp(
      title: 'Route Demo',
      initialRoute: '/',
      onGenerateRoute: (setting) {
        return MaterialPageRoute(
          builder: (context) {
            return pages[setting.name].call(setting.name, setting.arguments);
          },
          settings: setting,
        );
      },
      onGenerateInitialRoutes: (initialRoute) {
        if (initialRoute == '/') {
          return [
            MaterialPageRoute(
              builder: (context) {
                final keys = pages.keys.toList();
                return Scaffold(
                  appBar: AppBar(
                    title: Text('测试页面'),
                  ),
                  body: ListView.builder(
                    itemBuilder: (context, index) {
                      return ListTile(
                        onTap: () {
                          Navigator.pushNamed(context, keys[index]);
                        },
                        title: Text(keys[index]),
                      );
                    },
                    itemCount: keys.length,
                  ),
                );
              },
              settings: RouteSettings(name: '/'),
            )
          ];
        }
        return null;
      },
    );
  }
}
